<?php
namespace Home\Controller;
use Think\Controller;
class FastController extends Controller {

    //TP构造函数
    public function _initialize(){
        //自动加载的类
        new \Org\Autoload();
        if(session('member_id')==''){
            echo "<script>alert('您没有登录，请您登录后再访问此页面');window.location.href='/user/login';</script>";exit;
        }
        $this->member_id=$this->member_info['member_id'];
        //echo $member_id;exit;
    }

    public function index(){

        $data['title']='发布采购  找金银网';
        $data['active']='fast';
        $Product=D("Product");  //产品
        //$Member=D("Member");  //会员
        $Area=D("Area");  //品牌
        $Brand=D("Brand");  //品牌
        $Spec=D("Spec");  //规格
        $Unit=D("Unit");  //计量单位
        $cat_list=$Product->get_cat_list();
        //$member_list=$Member->get_member_list();
        $brand_list=$Brand->get_brand_list();
        $Area->type=2;  //地区列表 城市一级
        $area_list=$Area->get_area_list();
        $spec_list=$Spec->get_spec_list();
        $unit_list=$Unit->get_unit_list();
        //print_r($area_list);exit;
        $Member=D("Member");
        $Member->member_id=$this->member_id;
        $user_info=$Member->get_member_info();
        $this->assign("user_info", $user_info);
        $this->assign("data", $data);
        $this->assign("cat_list", $cat_list);
        $this->assign("brand_list", $brand_list);
        $this->assign("area_list", $area_list);
        $this->assign("spec_list", $spec_list);
        $this->assign("unit_list", $unit_list);

        $this->assign("data",$data);
        $this->display();
    }

    public function fast_add_post(){
        if(!$_POST['supply_title']||!$_POST['supply_desc']){
            echo "<script>alert('请您填写完整后再尝试重新提交');history.back(-1);</script>";exit;
        }
        $post=I('post.');
            $member_id=$post['member_id'];
            //echo $member_id;exit;
        if(!empty($_FILES['supply_image']['tmp_name'])){
            $root_path=ROOT_PATH.'www/data/upload/supply/'.$member_id;
            //echo $root_path;exit;
            if(!is_dir($root_path)){
                mkdir($root_path);
            }
            //上传图片
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize=2048000;// 设置附件上传大小
            $upload->exts=array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath=$root_path.'/'; // 设置附件上传根目录
            $upload->savePath=''; // 设置附件上传（子）目录
            $upload->subName='';
            $upload->saveName=md5(time());
            $info=$upload->upload();
            if(!$info) {// 上传失败
                echo "<script>alert('上传失败，请您重新上传');history.back(-1);</script>";exit;
            }
            $supply_image='/data/upload/supply/'.$member_id.'/'.$info['supply_image']['savename'];
            $post['supply_image']=$supply_image;
        }
        $post['member_id']=$this->member_id;
        $post['supply_time']=time();
        $post['supply_sn']=date('YmdHis',time()).rand(001,999);
        //print_r($post);exit;
        $Model=D("supply");
        $Model->post=$post;


        if(!$Model->supply_add_post()){
            echo "<script>alert('提交失败，请您尝试重新提交');history.back(-1);</script>";exit;
        }
        echo "<script>alert('提交成功');window.location.href='/Fast/'</script>";
    }
}